/*
 * @Description: 全宗reducer
 * @Author: Rfan
 * @Date: 2022-04-08 09:07:14
 * @LastEditTime: 2022-05-25 09:02:17
 */

import { jsonClone } from '@/utils';
import { delItemByIdArray, insertItem } from '@/utils/list';
import {
  ADD_RULE,
  DELETE_RULE,
  DELETE_RULE_USER,
  INIT_SELECTED_LIST,
  SAVE_USER_RIGHTS,
  SET_RULE_LIST,
  SET_RULE_SELECTED_ITEM,
  SET_RULE_USER_LIST,
  SET_SELECT_AT,
  SET_SELECT_FOND,
  SET_SELECT_LEVEL,
  SET_USER_RIGHTS_INFO,
  SET_USER_SELECTED_ITEM
} from '../constants';

export const UseRuleReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 表格加载loading
     */
    case SET_SELECT_FOND:
      return {
        ...state,
        selectFond: action.data
      };

    /**
     * @description: 设置选中的档案类型
     */
    case SET_SELECT_AT:
      return {
        ...state,
        selectAT: action.data
      };

    /**
     * @description: 设置选中的层级
     * @param {object} data 选中的层级
     */
    case SET_SELECT_LEVEL:
      return {
        ...state,
        selectLevel: action.data
      };

    /**
     * @description: 保存表格选中的条目id
     * @param {number[]} data
     */
    case SET_RULE_SELECTED_ITEM:
      return { ...state, ruleSelectItems: action.data };

    /**
     * @description: 保存表格选中的条目id
     * @param {number[]} data
     */
    case SET_USER_SELECTED_ITEM:
      return { ...state, userSelectItems: action.data };

    /**
     * @description: 设置规则列表
     * @param {object} data 规则列表
     */
    case SET_RULE_LIST:
      return {
        ...state,
        ruleList: action.data
      };

    /**
     * @description: 重置选中项目
     */
    case INIT_SELECTED_LIST:
      return {
        ...state,
        userList: [],
        userTableTitles: [],
        ruleSelectItems: [],
        userSelectItems: []
      };

    /**
     * @description: 新增规则到列表
     * @param {object} data 新增的规则规则
     */
    case ADD_RULE: {
      const ruleList = insertItem(action.data, state.ruleList);
      return {
        ...state,
        ruleList
      };
    }

    /**
     * @description: 删除规则到列表
     * @param {number|number[]} ids 删除规则ids
     */
    case DELETE_RULE: {
      const ruleList = delItemByIdArray(action.ids, state.ruleList);
      return { ...state, ruleList };
    }

    /**
     * @description: 设置规则下用户、组列表
     * @param {*} data 删除用户ids
     */
    case SET_RULE_USER_LIST: {
      return {
        ...state,
        userList: action.data?.entryList || [],
        userTableTitles: action.data?.operateRightsColList || []
      };
    }

    /**
     * @description: 删除规则下用户、组
     * @param {object} data 规则下的用户、组列表
     */
    case DELETE_RULE_USER: {
      const userList = delItemByIdArray(action.ids, state.userList, 'entryId');
      return { ...state, userList };
    }

    /**
     * @description: 设置弹窗，用户权限信息
     * @param {object} data 用户权限信息
     */
    case SET_USER_RIGHTS_INFO: {
      return {
        ...state,
        userRightsInfo: action.data
      };
    }

    /**
     * @description: 保存用户权限，根据回调刷新缓存数据，清空弹窗信息
     * @param {object} data 条目ID，及更改后的权限数组
     */
    case SAVE_USER_RIGHTS: {
      const cbObj = action.data;
      // 保存选项信息
      const options = jsonClone(state.userRightsInfo.operateRights.operateRightsOption);
      // 表格更改的条目
      const changeEntry = state.userList.find((item) => item.entryId === cbObj?.entryId);
      if (changeEntry && cbObj) {
        for (let i = 0; i < changeEntry.operateRightsList.length; i++) {
          const ele = changeEntry.operateRightsList[i];
          for (let j = 0; j < cbObj.operateRights.length; j++) {
            const element = cbObj.operateRights[j];
            // 循环表格的权限列表及保存接口返回的权限列表，更改对应项状态。通过选项信息找状态的名称(name)
            if (ele.index === element.index) {
              ele.value = element.value;
              ele.name = options.find((option) => option.value === element.value)?.name;
              break;
            }
          }
        }
      }
      return {
        ...state,
        userRightsInfo: {},
        userList: state.userList.map((item) => {
          return item.entryId === changeEntry.entryId ? changeEntry : item;
        })
      };
    }

    default:
      console.log('untouched', action);
      return state;
  }
};
